import 'package:intl/intl.dart';

class DateUtil{
  //当前时间
  static String getCurrentTime(){
    return DateFormat("yyyy-MM-dd HH:mm:ss").format(DateTime.now());
  }

  //日期转字符串
  static String format(date,{format:"yyyy-MM-dd HH:mm:ss"}){
    return DateFormat(format).format(date!=null?date:DateTime.now());
  }

  //字符串转日期
  static DateTime dateTimeParse(dateStr,{format:"yyyy-MM-dd HH:mm:ss"}){
    return DateFormat(format).parse(dateStr);
  }

  //毫秒时间戳转日期
  static DateTime dateTimeByMicroseconds(microsecondsSinceEpoch){
    return DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch);
  }

  //几天前开始时间
  static DateTime dateBeginTime(DateTime date,{int beforeDay,int afterDay}){
    if(beforeDay != null){
      return dateTimeParse(format(date.add(Duration(seconds: -beforeDay*24*60*60)),format: "yyyy-MM-dd")+" 00:00:00");
    }else if(afterDay != null){
      return dateTimeParse(format(date.add(Duration(seconds: afterDay*24*60*60)),format: "yyyy-MM-dd")+" 00:00:00");
    }
    return dateTimeParse(format(date,format: "yyyy-MM-dd")+" 00:00:00");
  }

  //几天后截止时间23:59:59
  static DateTime dateEndTime(DateTime date,{int beforeDay,int afterDay}){
    if(beforeDay != null){
      return dateTimeParse(format(date.add(Duration(seconds: -beforeDay*24*60*60)),format: "yyyy-MM-dd")+" 23:59:59");
    }else if(afterDay != null){
      return dateTimeParse(format(date.add(Duration(seconds: afterDay*24*60*60)),format: "yyyy-MM-dd")+" 23:59:59");
    }
    return dateTimeParse(format(date,format: "yyyy-MM-dd")+" 23:59:59");
  }


}